﻿# 待安装工具列表
$tools = @("git", "curl", "make", "kind")

# 检查是否以普通用户身份运行（可选）
if ([Security.Principal.WindowsIdentity]::GetCurrent().Groups -contains 'S-1-5-32-544') {
    Write-Host "注意：您正在以管理员身份运行此脚本"
    # 如果不想退出，可以注释掉下面两行
    # Write-Error "请不要以管理员身份运行此脚本"
    # exit 1
}

# 设置执行策略
Write-Host "=== 检查 PowerShell 执行策略 ==="
$currentPolicy = Get-ExecutionPolicy -Scope CurrentUser
if ($currentPolicy -ne "RemoteSigned") {
    Write-Host "设置执行策略为 RemoteSigned..."
    Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
} else {
    Write-Host "执行策略已经是 RemoteSigned"
}

# 检查 Scoop 是否已安装
if (!(Get-Command scoop -ErrorAction SilentlyContinue)) {
    Write-Host "=== 安装 Scoop ==="
    irm get.scoop.sh | iex
} else {
    Write-Host "Scoop 已安装"
}

# 配置镜像源
Write-Host "`n=== 检查镜像源配置 ==="
$mainBucket = scoop bucket list | Where-Object { $_.Name -eq "main" }
$currentRepo = scoop config SCOOP_REPO

if ($mainBucket.Source -ne "https://mirror.nju.edu.cn/git/scoop-main.git") {
    Write-Host "配置 main bucket 镜像源..."
    scoop bucket rm main 2>$null
    scoop bucket add main https://mirror.nju.edu.cn/git/scoop-main.git
} else {
    Write-Host "main bucket 镜像源已配置"
}

if ($currentRepo -ne "https://mirror.nju.edu.cn/git/scoop-main.git") {
    Write-Host "配置 SCOOP_REPO 镜像源..."
    scoop config SCOOP_REPO https://mirror.nju.edu.cn/git/scoop-main.git
} else {
    Write-Host "SCOOP_REPO 镜像源已配置"
}

# 添加 extras 仓库
Write-Host "`n=== 检查 extras 仓库 ==="
$extrasBucket = scoop bucket list | Where-Object { $_.Name -eq "extras" }
if (!$extrasBucket) {
    Write-Host "添加 extras 仓库..."
    scoop bucket add extras https://mirror.nju.edu.cn/git/scoop-extras.git
} else {
    if ($extrasBucket.Source -ne "https://mirror.nju.edu.cn/git/scoop-extras.git") {
        Write-Host "更新 extras 仓库镜像源..."
        scoop bucket rm extras 2>$null
        scoop bucket add extras https://mirror.nju.edu.cn/git/scoop-extras.git
    } else {
        Write-Host "extras 仓库已正确配置"
    }
}

# 安装必要工具
Write-Host "`n=== 安装必要工具 ==="
foreach ($tool in $tools) {
    if (!(Get-Command $tool -ErrorAction SilentlyContinue)) {
        Write-Host "正在安装 $tool..."
        scoop install $tool
    } else {
        Write-Host "$tool 已安装"
    }
}

# 验证安装
Write-Host "`n=== 验证安装 ==="
Write-Host "已安装的工具版本："
foreach ($tool in $tools) {
    Write-Host "$tool version: $((Get-Command $tool).Version)"
}

Write-Host "`n=== 安装完成 ==="
Write-Host "请重新打开终端以使更改生效"

# 添加暂停命令，等待用户按任意键后再关闭
Write-Host "`n按任意键继续..."
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')